<template>
  <div class="person-message">
    <div class="message">
      <div class="img">
        <el-upload class="avatar-uploader" action="/api/avatar" :headers = "imgheader" :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
        <img v-if="imageUrl" :src="imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
      </div>

      <p v-if="userInfo.name">{{userInfo.name}}</p>
      <p v-else>请编辑昵称</p>
      <p>余额:{{userInfo.money}}</p>
      <a @click="changeName" href="javascript:;"><span>编辑名称</span></a>
    </div>
    <p><span>手机号码：</span>{{userInfo.phone}}</p>
    <p><span>上次登录：</span>{{userInfo.updated_at}}</p>
  </div>
</template>

<script>
import Axios from "@/common/js/Axios";
export default {
  mounted() {
    this.getUserInfo();
  },
  data() {
    return {
      token: localStorage.getItem("Authorization"),
      //用户信息
      userInfo: {},
      imageUrl: "",
      //头像上传
      imgheader : {
        "Authorization" : "Bearer "+localStorage.getItem("Authorization")
      },
    };
  },
  methods: {
    //获取用户信息
    getUserInfo() {
      Axios.getAxios(
        "/api/user/",
        {},
        {
          Authorization: "Bearer " + this.token
        }
      ).then(
        res => {
          if (res) {
            this.userInfo = res.data.data;
            this.imageUrl = res.data.data.image;
          } else {
            this.$message.error("登录失败，请重新登录");
          }
        },
        err => {
          this.$message("请登录查看更多页面");
        }
      );
    },
    //修改头像

    changeTx() {},
    //上传后的事件
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.$message("上传成功");
    },
    //检测上传
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG && !isPNG) {
        this.$message.error("上传头像图片只能是 JPG或者PNG 格式!");
        return false;
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
        return false;
      }
      console.log(file);
      this.file = file;
      return (isJPG || isPNG) && isLt2M;
    },
    //修改昵称
    changeName() {
      this.$prompt("请输入需要设置的昵称", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消"
      })
        .then(({ value }) => {
          Axios.postAxios(
            "/api/changeuserdata",
            {
              type: "name",
              data: value
            },
            {
              Authorization: "Bearer " + this.token
            }
          ).then(
            res => {
              this.$message.success("修改成功");
              this.getUserInfo();
            },
            err => {
              this.$message.error("修改失败");
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入"
          });
        });
    }
  }
};
</script>

<style lang="stylus" scoped>
.img{
    width: 80px;
    height: 80px;
    border-radius: 50%;
    margin: 0 auto;
    img{
      width 100%;
      border-radius:10px;
    }
}
</style>

